<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

require_once LIB_PATH . "Library/TaobaoAPI/Curl.php";
class SynchronizeAction extends NewzanAction
{
	protected $userId = null;
	protected static $allowMothod = array(
			'campaign', 'adgroup', 'keyword', 'products', 'rpt', 'rpt_shop'
		);

	protected static $p = array();

	public function _initialize()
	{
		parent::_initialize();
		$this->userId = $this->me['user_id'];
		self::$p = array(
			'start_date' => date('Y-m-d', strtotime('-7 day')),
			'end_date' => date('Y-m-d', strtotime('-1 day'))
		);
		$this->http = new Curl();
	}

	public function index()
	{
		$this->display();
	}

	protected function checkData($data)
	{
		if(!in_array($data, self::$allowMothod))
			$this->display_json(0);
		else
			return $data;
	}

	public function update()
	{
		$rel = $this->checkData($this->_get('rel'));
		if($rel == 'keyword') {
			$p = $this->_get('p');
			$this->$rel($p);
		} else {
			$this->$rel();
		}
		
		$this->display_json(1);
	}

	protected function campaign()
	{
		$url = build_url("simbasync/tenants/{$this->userId}");
		$this->http->get($url);

		return 1;
	}

	protected function adgroup()
	{
		$urlBase = "simbasync/tenants/{$this->userId}/";
		
		$campaign_iids = $this->getData('campaign');

		if($campaign_iids) {
			foreach ($campaign_iids as $campaign) {
				// 推广组
				$url_adgroup = build_url($urlBase . "campaigns/{$campaign['campaign_id']}/@adgroup");
				$this->http->get($url_adgroup);

				$adgroup_iids = $this->getData('adgroup', $campaign['campaign_id']);
				
				if($adgroup_iids) {
					foreach ($adgroup_iids as $adgroup) {
						// 关键词
						// $url_keyword = build_url($urlBase . "adgroups/{$adgroup['adgroup_id']}/@keyword");
						// $this->http->get($url_keyword);
						$this->keyword($adgroup['adgroup_id']);

						// 创意
						$url_creative = build_url($urlBase . "adgroups/{$adgroup['adgroup_id']}");
						$this->http->get($url_keyword);
					}
				}
			}
		}

		return 1;
	}

	protected function products()
	{
		$url = build_url("simbasync/tenants/{$this->userId}/@product");
		$this->http->get($url);

		return 1;
	}

	protected function keyword($adgroup_id)
	{
		$url = build_url("simbasync/tenants/{$this->userId}/adgroups/{$adgroup_id}/@keyword");
		$this->http->get($url);

		return 1;
	}

	protected function rpt()
	{
		$urlBase = "simbasync/tenants/{$this->userId}/";

		$this->rpt_shop();

		$url_campaign = build_url($urlBase . '@rptcampaign', self::$p);
		$this->http->get($url_campaign);

		$campaign_iids = $this->getData();
		if($campaign_iids) {
			foreach ($campaign_iids as $campaign) {
				$adgroup_iids = $this->getData('adgroup', $campaign['campaign_id']);

				if(!$adgroup_iids)
					continue;
				
				foreach ($adgroup_iids as $adgroup) {
					$url_adgroup = build_url($urlBase . "campaigns/{$campaign['campaign_id']}/@rptadgroup", self::$p);
					$this->http->get($url_adgroup);

					$url_creative = build_url($urlBase . "campaigns/{$campaign['campaign_id']}/adgroups/{$adgroup['adgroup_id']}", self::$p);
					$this->http->get($url_creative);

					$url_keyword = build_url($urlBase . "campaigns/{$campaign['campaign_id']}/adgroups/{$adgroup['adgroup_id']}/@rptkeyword", self::$p);
					$this->http->get($url_keyword);
				}
			}
		}
		return 1;
	}

	private function getData($type = 'campaign', $id = '')
	{
		$table = $type . 's';
		$where = array(
				'user_id'=>$this->userId
			);

		if($type == 'adgroup')
			$where['campaign_id'] = $id;
		return M($table)->field(($type == 'campaign' ? 'campaign_id' : 'adgroup_id'))->where($where)->select();
	}

	private function rpt_shop()
	{
		$url = build_url("simbasync/tenants/{$this->userId}/@rptcustomer", array(
			'start_date' => date('Y-m-d', strtotime('-20 day')),
			'end_date' => date('Y-m-d', strtotime('-1 day'))
		));
		$this->http->get($url);
		return 1;
	}

	public function auto()
	{
		$this->user_id = $this->me['user_id'];
		$this->display();
	}
}
